package yl.hs.bmipfull.controllers.production;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.production.ProductionTaskExtendInfoEntity;
import yl.hs.bmipfull.pojo.salemanage.SaleOrderEntity;
import yl.hs.bmipfull.service.production.IProductionTaskFertilizerService;
import yl.hs.bmipfull.service.production.IProductionTaskService;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.TaskState;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 作用:  生产任务表控制层<br/>
 * 创建时间: 2021年05月26日 11时23分11秒 <br/>
 *
 * @author 张凯
 */
@Api(value = "生产任务表Web接口", tags = "生产管理——生产任务表操作")
@RequestMapping(value = "/api/production")
@RestController
public class ProductionTaskController extends ControllerBase {

    @Autowired
    IProductionTaskService service;
    @Autowired
    IProductionTaskFertilizerService fertilizerService;
    @ApiOperation(value = "查询生产任务表", notes = "分页查询生产任务表")
    @RequestMapping(value = "task/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPage(query, rowNum, page);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }


    @ApiOperation(value = "查询生产任务表", notes = "根据生产任务表编号查询生产任务表")
    @RequestMapping(value = "task/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", service.findBySnToMap(sn));
    }

    @ApiOperation(value = "查询生产任务表", notes = "根据生产任务表编号查询生产任务表")
    @RequestMapping(value = "task_data/{sn}", method = RequestMethod.GET)
    public WebResult findDataBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", service.findBySnToMap(sn));
    }

    @ApiOperation(value = "查询生产任务表", notes = "根据状态编号查询生产任务表")
    @RequestMapping(value = "task/findByState", method = RequestMethod.POST)
    public WebResult findByState(@ApiParam(value = "状态") @RequestBody PageQuery pageQuery) {
        return success("查询成功", service.getTaskByState(pageQuery, TaskState.IN_PRODUCTION.code));
    }


    @ApiOperation(value = "保存生产任务表", notes = "查询/更新生产任务表信息")
    @RequestMapping(value = "task", method = RequestMethod.PUT)
    public WebResult saveTask(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            List<Map<String, Object>> datas = (List<Map<String, Object>>) submitModel.getData().get("PumpTruckList");
            List<ProductionTaskExtendInfoEntity> itemList = new ArrayList<>();
            if (datas != null && datas.size() > 0) {
                for (var item : datas) {
                    ProductionTaskExtendInfoEntity entity = new ProductionTaskExtendInfoEntity();
                    Tl.getChangeLog(entity, item);
                    itemList.add(entity);
                }
            }
            result = service.saveProductionTask(submitModel.getData(), itemList, submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "根据sn更改生产任务状态", notes = "根据sn更改生产任务状态")
    @RequestMapping(value = "task/update/{sn}/{state}", method = RequestMethod.GET)
    public WebResult updateTask(@ApiParam(value = "生产任务sn") @PathVariable String sn,
                                @ApiParam(value = "生产任务状态") @PathVariable Short state) {
        if (service.updateTask(sn, state) > 0) {
            return success("修改成功");
        } else {
            return fail("修改失败");
        }
    }
    @ApiOperation(value = "根据编号修改状态", notes = "根据编号修改状态")
    @RequestMapping(value = "task/changeStateBySn/{sn}/{state}", method = RequestMethod.GET)
    @ResponseBody
    public WebResult changeStateBySn(@PathVariable String sn,@PathVariable short state) {
        try {
            Integer result = service.changeStateBySn(sn.split(","), state);
            if (result > 0) {
                return success("数据已修改");
            } else if (result == -2) {
                return fail("数据修改失败！");
            }
            return fail("没有修改的任何数据");
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "删除生产任务表信息", notes = "根据生产任务表编号删除系生产任务表信息")
    @RequestMapping(path = "task/{SN}", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "生产任务表编号", name = "SN", allowMultiple = true, required = true) @PathVariable String SN) {
        if (service.delete(SN.split(",")) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }
    @ApiOperation(value = "根据销售订单信息创建对应生产任务", notes = "根据销售订单信息创建对应生产任务")
    @RequestMapping(path = "task/savebysaleorder", method = RequestMethod.PUT)
    public WebResult saveBySaleOrder(@ApiParam(value = "提交信息") @RequestBody List<String> data){
        fertilizerService.saveTaskBySaleOrder(data);
        return success("");
    }
}